<?php 
class Rpc {
	static public function em_candispatch() {
	  return isset($_REQUEST['fn']) && $fn = $_REQUEST['fn'];
	}
	
	static public function em_rpc_dispatch($get_obj) {
		if(isset($_REQUEST['fn']) && $fn = $_REQUEST['fn']) {
			if(isset($_REQUEST['params'])) {
				$params = unserialize(urldecode($_REQUEST['params']));
				if(!is_array($params)) $params = array();
			} else {
				$params = array();
			}
			$handler = $get_obj;
			try {
				$result = array(0, call_user_func_array(array($handler,$fn),$params));
			} catch (Exception $e) {
				$result = array(1, $e);
			}
			
			echo serialize($result);
		}
	}
	
	static public function em_rpc($url, $fn, $debug = false) {
		global $server_api;
		$args = func_get_args();
		$args = array_slice($args, 2);
		$response = Curl::curl_post($url.$server_api,array(
				'fn'=>$fn,
				'params'=>urlencode(serialize($args))
		));
	
		$result = @unserialize($response);
	
		if(is_array($result)) {
			if($result[0] == 0)
				return $result[1];
			else
				throw new Exception('Remote Exception:',0,$result[1]);
		}
		throw new Exception("Remote Incorrect Response:".strip_tags($response));
	}
}
?>